<template>
  <div class="login">
    <div class="login-warp">
      <div class="login-box">
        <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="" class="demo-ruleForm">
          <div class="title-container">
            <h3 class="title">文章管理系统</h3>
          </div>
          <el-form-item label="" prop="user_name">
            <el-input v-model.number="ruleForm.user_name" placeholder="请输入用户名">
              <template #prefix>
                <el-icon class="el-input__icon">
                  <avatar />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="" prop="pass_word">
            <el-input v-model="ruleForm.pass_word" type="password" placeholder="请输入密码" show-password>
              <template #prefix>
                <el-icon class="el-input__icon">
                  <lock />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item class="btn-item">
            <el-button type="primary" @click="submitForm()" class="btn">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script lang="ts">
import { login } from '@/api/user'
import { defineComponent, reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { Avatar, Lock } from '@element-plus/icons'
import {useRouter } from 'vue-router'
interface ruleData {
  required: Boolean
  message: string
  trigger: string
}
interface iData {
  ruleForm: {
    user_name: string
    pass_word: string
  }
  rules: {
    user_name: ruleData[]
    pass_word: ruleData[]
  }
}
interface form {
  user_name: string
  pass_word: string
}
export default defineComponent({
  name: 'Login',
  components: { Avatar, Lock },
  setup() {
    const ruleFormRef = ref('ruleForm')
    const router = useRouter();
    const ruleForm: form = reactive({
      user_name: '',
      pass_word: '',
    })
    const rules = {
      user_name: [
        {
          required: true,
          message: '请输入用户名',
          trigger: 'blur',
        },
      ],
      pass_word: [
        {
          required: true,
          message: '请输入密码',
          trigger: 'blur',
        },
      ],
    }
    const submitForm = () => {
      const ruleFormRefValue:any = ruleFormRef.value
      ruleFormRefValue.validate(async (valid: boolean) => {
        if (!valid) return
        const { data } = await login(ruleForm)
        if (data.code === 200) {
          const userInfo = data.result
          sessionStorage.setItem('token', userInfo.token)
          sessionStorage.setItem('userInfo', JSON.stringify(userInfo))
          router.replace('/home')
        } else {
          ElMessage({
            message: '登录失败',
            type: 'warning',
            offset: 330
          })
        }
      })
    }
    return {
      ruleForm,
      submitForm,
      ruleFormRef,
      rules,
    }
  },
})
</script>
<style lang="scss" scoped>
.login {
  background: url(@/assets/images/login.jpg) no-repeat;
  background-size: cover;
  height: 100%;
  position: relative;
}
.login-warp {
  width: 400px;
  height: 300px;
  background-color: #fff;
  position: absolute;
  left: 50%;
  top: 40%;
  transform: translate(-50%, -50%);
}
.login-box {
  margin-top: 20px;
  padding: 0 20px;
}
.btn-item {
  text-align: center;
}
.title-container {
  text-align: center;
  margin-bottom: 20px;
}
.btn {
  width: 100%;
}
.title {
  font-size: 24px;
  color: rgba(0, 0, 0, 0.45);
}
.el-form {
    display: block;
}
</style>